﻿//-----------------------------------------------------------------------
// <copyright file="PublishedFaultExceptionOfT.cs" company="CodePlex">
//     Copyright (c) Doug Holland, CodePlex. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace CodePlex.Diagnostics
{
    using System;

    using System.Runtime.Serialization;

    using System.Security;
    using System.Security.Permissions;

    using System.ServiceModel;
    using System.ServiceModel.Channels;

    /// <summary>
    /// Represents a SOAP fault that has been successfully published.
    /// </summary>
    /// <typeparam name="T">The serializable error detail type.</typeparam>
    [Serializable]
    public class PublishedFaultException<T> : FaultException<T>, IPublishedException
    {
        #region public PublishedFaultException(Guid publicationId, T detail) : base(detail)

        /// <summary>
        /// Initializes a new instance of the <see cref="T:PublishedFaultException`1"/> class that uses the specified detail object.
        /// </summary>
        /// <param name="publicationId">The publication identifier of the SOAP fault.</param>
        /// <param name="detail">The object used as the SOAP fault detail.</param>
        public PublishedFaultException(Guid publicationId, T detail) : base(detail)
        {
            this.PublicationId = publicationId;
        }

        #endregion

        #region public PublishedFaultException(Guid publicationId, T detail, FaultReason reason) : base(detail, reason)

        /// <summary>
        /// Initializes a new instance of the <see cref="T:PublishedFaultException`1"/> class that uses the specified detail object and fault reason.
        /// </summary>
        /// <param name="publicationId">The publication identifier of the SOAP fault.</param>
        /// <param name="detail">The object used as the SOAP fault detail.</param>
        /// <param name="reason">The reason for the SOAP fault.</param>
        public PublishedFaultException(Guid publicationId, T detail, FaultReason reason) : base(detail, reason)
        {
            this.PublicationId = publicationId;
        }

        #endregion

        #region public PublishedFaultException(Guid publicationId, T detail, string reason) : base(detail, reason)

        /// <summary>
        /// Initializes a new instance of the <see cref="T:PublishedFaultException`1"/> class that uses the specified detail object and fault reason
        /// </summary>
        /// <param name="publicationId">The publication identifier of the SOAP fault.</param>
        /// <param name="detail">The object used as the SOAP fault detail.</param>
        /// <param name="reason">The reason for the SOAP fault.</param>
        public PublishedFaultException(Guid publicationId, T detail, string reason) : base(detail, reason)
        {
            this.PublicationId = publicationId;
        }

        #endregion

        #region public PublishedFaultException(Guid publicationId, T detail, FaultReason reason, FaultCode code) : base(detail, reason, code)

        /// <summary>
        /// Initializes a new instance of the <see cref="T:PublishedFaultException`1"/> class that uses the specified detail object, fault reason, and fault code.
        /// </summary>
        /// <param name="publicationId">The publication identifier of the SOAP fault.</param>
        /// <param name="detail">The object used as the SOAP fault detail.</param>
        /// <param name="reason">The reason for the SOAP fault.</param>
        /// <param name="code">The fault code of the SOAP fault.</param>
        public PublishedFaultException(Guid publicationId, T detail, FaultReason reason, FaultCode code) : base(detail, reason, code)
        {
            this.PublicationId = publicationId;
        }

        #endregion

        #region public PublishedFaultException(Guid publicationId, T detail, string reason, FaultCode code) : base(detail, reason, code)

        /// <summary>
        /// Initializes a new instance of the <see cref="T:PublishedFaultException`1"/> class that uses the specified detail object, fault reason, and fault code.
        /// </summary>
        /// <param name="publicationId">The publication identifier of the SOAP fault.</param>
        /// <param name="detail">The object used as the SOAP fault detail.</param>
        /// <param name="reason">The reason for the SOAP fault.</param>
        /// <param name="code">The fault code of the SOAP fault.</param>
        public PublishedFaultException(Guid publicationId, T detail, string reason, FaultCode code) : base(detail, reason, code)
        {
            this.PublicationId = publicationId;
        }

        #endregion

        #region public PublishedFaultException(Guid publicationId, T detail, FaultReason reason, FaultCode code, string action) : base(detail, reason, code, action)

        /// <summary>
        /// Initializes a new instance of the <see cref="T:PublishedFaultException`1"/> class that uses the specified detail object, fault reason, fault code, and action values.
        /// </summary>
        /// <param name="publicationId">The publication identifier of the SOAP fault.</param>
        /// <param name="detail">The object used as the SOAP fault detail.</param>
        /// <param name="reason">The reason for the SOAP fault.</param>
        /// <param name="code">The fault code of the SOAP fault.</param>
        /// <param name="action">The action of the SOAP fault.</param>
        public PublishedFaultException(Guid publicationId, T detail, FaultReason reason, FaultCode code, string action) : base(detail, reason, code, action)
        {
            this.PublicationId = publicationId;
        }

        #endregion

        #region public PublishedFaultException(Guid publicationId, T detail, string reason, FaultCode code, string action) : base(detail, reason, code, action)

        /// <summary>
        /// Initializes a new instance of the <see cref="T:PublishedFaultException`1"/> class that uses the specified detail object, fault reason, fault code, and action values.
        /// </summary>
        /// <param name="publicationId">The publication identifier of the SOAP fault.</param>
        /// <param name="detail">The object used as the SOAP fault detail.</param>
        /// <param name="reason">The reason for the SOAP fault.</param>
        /// <param name="code">The fault code of the SOAP fault.</param>
        /// <param name="action">The action of the SOAP fault.</param>
        public PublishedFaultException(Guid publicationId, T detail, string reason, FaultCode code, string action) : base(detail, reason, code, action)
        {
            this.PublicationId = publicationId;
        }

        #endregion

        #region protected PublishedException(SerializationInfo info, StreamingContext context) : base(info, context)

        /// <summary>
        /// Initializes a new instance of the <see cref="T:PublishedFaultException`1"/> class.
        /// </summary>
        /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
        /// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext"/> that contains contextual information about the source or destination.</param>
        /// <exception cref="T:System.Runtime.Serialization.SerializationException">The class name is <c>null</c> or <see cref="P:System.Exception.HResult"/> is zero (0).</exception>
        /// <exception cref="T:System.ArgumentNullException">The info parameter is <c>null</c>.</exception>
        [SecurityPermission(SecurityAction.LinkDemand, SerializationFormatter = true)]
        protected PublishedFaultException(SerializationInfo info, StreamingContext context) : base(info, context)
        {
            // retrieve the publication identifier.
            this.PublicationId = (Guid)info.GetValue(SR.PublicationId, typeof(Guid));
        }

        #endregion

        #region public Guid PublicationId

        /// <summary>
        /// Gets the publication Id associated with the fault.
        /// </summary>
        public Guid PublicationId
        {
            get;
            private set;
        }

        #endregion

        #region public override void GetObjectData(SerializationInfo info, StreamingContext context)

        /// <summary>
        /// Sets the <see cref="T:System.Runtime.Serialization.SerializationInfo"/> with information about the exception.
        /// </summary>
        /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
        /// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext"/> that contains contextual information about the source or destination.</param>
        /// <exception cref="T:System.ArgumentNullException">The info parameter is a <c>null</c> reference (Nothing in Visual Basic). </exception>
        /// <PermissionSet>
        /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Read="*AllFiles*" PathDiscovery="*AllFiles*"/>
        /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="SerializationFormatter"/>
        /// </PermissionSet>
        [SecurityPermission(SecurityAction.LinkDemand, SerializationFormatter = true)]
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);

            if (info != null)
            {
                // store the publication identifier.
                info.AddValue(SR.PublicationId, this.PublicationId);
            }
        }

        #endregion
    }
}
